<?php if (!defined('TL_ROOT')) die('You can not access this file directly!');

/**
 * TYPOlight webCMS
 * Copyright (C) 2005 Leo Feyer
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program. If not, please visit the Free
 * Software Foundation website at http://www.gnu.org/licenses/.
 *
 * PHP version 5
 * @copyright  Softleister 2008 
 * @author     Softleister <info@softleister.de> 
 * @package    Ticker 
 * @license    LGPL 
 * @filesource
 */

/**
 * Class ModuleTicker 
 *
 * @copyright  Softleister 
 * @author     Softleister 
 * @package    Extension Ticker
 */
class ModuleTicker extends Module
{
	protected $strTemplate = 'mod_ticker';

	/**
	 * Target pages
	 * @var array
	 */
	protected $arrTargets = array();

	/**
	 * Display a wildcard in the back end
	 * @return string
	 */
	public function generate()
	{
	  if( TL_MODE == 'BE' ) {
	    $this->Template = new BackendTemplate('be_wildcard');
	    $this->Template->wildcard = '### TICKER ###';

	    return $this->Template->parse();
	  }

	  $this->ticker_categories = deserialize($this->ticker_categories, true);

	  // Return if there are no categories
	  if( !is_array($this->ticker_categories) || count($this->ticker_categories) < 1 ) {
	    return '';
	  }
	  return parent::generate();
	}


	/**
	 * Generate module
	 */
	protected function compile()
	{
	  $arrTicker = array();
	  $objTicker = $this->Database->execute("SELECT tl_ticker.id AS id, pid, tickertext, jumpTo, parameter, speed, wait FROM tl_ticker LEFT JOIN tl_ticker_category ON(tl_ticker_category.id=tl_ticker.pid) WHERE pid IN(" . implode(',', $this->ticker_categories) . ")" . (!BE_USER_LOGGED_IN ? " AND published=1" : "") . " ORDER BY title, pid, sorting");
	  
	  while( $objTicker->next() ) {
	    if( $objTicker->parameter == 1 ) {    // angepasstes Verhalten ?
	      $speed = $objTicker->speed; $wait = $objTicker->wait;
	    }
	    else {
	      $speed = 60; $wait = 3000;
	    }
	    $arrTicker[] = array (
	      'tickertext' => $objTicker->tickertext,
	      'tickerid'   => 'ticker'.$objTicker->pid,
	      'speed'      => $speed,
	      'wait'       => $wait,
	      'jumpTo'     => $this->generateTickerLink( $objTicker )
	    );
	  }	
	  $this->Template->ticker = $arrTicker;
	}

	/**
	 * Create links and remember pages that have been processed
	 * @param object
	 * @return string
	 */
	private function generateTickerLink( Database_Result $objTicker )
	{
	  if( !array_key_exists( $objTicker->id, $this->arrTargets ) ) {
	    if( $objTicker->jumpTo < 1 ) {
	      return '';
//	      $this->arrTargets[$objTicker->id] = ampersand($this->Environment->request, ENCODE_AMPERSANDS);
	    }
	    else {
	      $objTarget = $this->Database->prepare("SELECT id, alias FROM tl_page WHERE id=?")
					  ->limit(1)
					  ->execute( intval($objTicker->jumpTo) );

	      if( $objTarget->numRows < 1 ) {
		$this->arrTargets[$objTicker->id] = ampersand($this->Environment->request, ENCODE_AMPERSANDS);
	      }

	      $this->arrTargets[$objTicker->id] = ampersand($this->generateFrontendUrl($objTarget->fetchAssoc(), '/items/' . $objTicker->id));
	    }
	  }
	  return $this->arrTargets[$objTicker->id];
	}

}

?>